<template>
  <div>
    <div class="player-box">
      <div class="player-box-contents">
        <div>
          <div class="player-box-left" @click="play" v-if="!isPlay">
            <div class="right-top-play">
              <i class="doorIcon">&#xe672;</i>
              <!-- <i class="doorIcon" v-else>&#xe671;</i> -->
            </div>
          </div>
          <div class="player-box-left" @click="play" v-else>
            <div class="solid-item" style="margin-right: 2px" :class="{ 'solid-1': isPlay }"></div>
            <div class="solid-item" style="margin-right: 2px" :class="{ 'solid-2': isPlay }"></div>
            <div class="solid-item" style="margin-right: 2px" :class="{ 'solid-3': isPlay }"></div>
            <div class="solid-item" style="margin-right: 2px" :class="{ 'solid-4': isPlay }"></div>
            <div class="solid-item" style="margin-right: 2px" :class="{ 'solid-5': isPlay }"></div>
          </div>
        </div>
        <div class="player-box-right">
          <div class="right-top">
            <span class="audio-name">{{ props.audio.name }}</span>
            <div class="right-top-menu">
              <div class="edit-box" v-if="props.editType" @click="edit">
                <i class="doorIcon leftIcon" @click="edit">&#xe65d;</i>
                <span>编辑</span>
              </div>
              <!-- <t-popup
                attach="#menu-left-btn-xe662" 
                overlayClassName="popup" 
                class="placement top"  
                content="删除音频"
                placement="top" 
                showArrow  
                destroyOnClose
              ></t-popup> -->
              <t-popconfirm content="确认删除吗" theme="warning" @confirm="del">
                <div class="edit-box">
                  <i class="doorIcon leftIcon">&#xe634;</i>
                  <span>删除</span>
                </div>
              </t-popconfirm>
            </div>
          </div>
          <div class="right-description">
            <span> {{ props.audio.description }}</span>
          </div>
        </div>
      </div>
      <div class="right-bottom">
        <div class="right-bottom-left">{{ realMusicTime }}</div>
        <div class="right-bottom-center" @mousedown="mousedown" @mouseup="mouseup">
          <t-slider v-model="percentage" style="width: 100%" />
        </div>
        <div class="right-bottom-right">{{ totalMusicTime }}</div>
      </div>
    </div>
    <audio :src="urls + props.audio.url" ref="audioRef" v-show="false" />
  </div>
</template>

<script setup lang="ts">
// @ts-nocheck
import { urls } from '@/utils/request/upload';
import { ref, onMounted, nextTick, reactive } from 'vue';

const emit = defineEmits(['delAudioUrl', 'editAudioUrl']);
const audioRef = ref(null);
const isPlay = ref(false);
const realMusicTime = ref('00:00');
const totalMusicTime = ref('00:00');
const percentage = ref(0);
const move = ref(true);
const props = defineProps({
  audio: {
    type: Object,
  },
  editType: {
    type: Boolean,
  },
  info: {
    type: Object,
  },
});

// default: {
//       type: 3,
//       url: '/369462942949445/audio/20230829/455210145833029.mp3',
//       description: '',
//       name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
//     },
const audio = reactive({
  type: 3,
  url: '/369462942949445/audio/20230829/455210145833029.mp3',
  description: '',
  name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
});

// 处理进度条
const del = () => {
  emit('delAudioUrl');
};
const edit = () => {
  emit('editAudioUrl');
};
const handMusicBar = () => {
  let duration = audioRef.value.duration;
  let x = ((audioRef.value.currentTime / duration) * 100).toFixed(1);
  if (move.value) {
    percentage.value = x;
  }

  // console.log(x, '当前播放百分比');
};

// 处理时间
const handlMusicTime = () => {
  //用秒数来显示当前播放进度
  let timeDisplay = Math.floor(audioRef.value.currentTime); //获取实时时间
  //分钟
  let minute = parseInt(timeDisplay / 60);
  if (minute < 10) {
    minute = '0' + minute;
  }
  //秒
  let second = Math.round(timeDisplay % 60);
  if (second < 10) {
    second = '0' + second;
  }
  realMusicTime.value = minute + ':' + second;
};

const watchMusicTime = () => {
  let music = audioRef.value;

  music.addEventListener(
    'timeupdate',
    () => {
      handlMusicTime();
      nextTick(() => {
        handMusicBar();
      });
    },
    false,
  );
  // 播放完毕
  music.addEventListener('ended', () => {
    //this.switchMusic(); // 自动播放
  });
  // 捕获音频文件已准备完毕
  // 当媒体文件可以播放的时候会触发oncanplay事件,也可以用oncanplay
  music.oncanplaythrough = () => {
    let time = audioRef.value.duration;
    //分钟
    let minutes = parseInt(time / 60);
    if (minutes < 10) {
      minutes = '0' + minutes;
    }
    //秒
    let seconds = Math.round(time % 60);
    if (seconds < 10) {
      seconds = '0' + seconds;
    }
    totalMusicTime.value = minutes + ':' + seconds;
  };
};
// 处理点击进度条事件
const handClickBar = (e) => {
  audioRef.value.currentTime = (percentage.value / 100) * audioRef.value.duration; // 计算播放时间 位置百分比*总时间
  nextTick(() => {
    audioRef.value.play();
    isPlay.value = true;
  });
};
const mousedown = () => {
  move.value = false;
};
const mouseup = () => {
  handClickBar();
  move.value = true;
};
const play = () => {
  if (audioRef.value.paused) {
    audioRef.value.play();
    isPlay.value = true;
  } else {
    audioRef.value.pause();
    isPlay.value = false;
  }
};
onMounted(() => {
  watchMusicTime();
});
</script>

<style scoped lang="less">
.player-box {
  display: flex;
  flex-direction: column;
  max-width: 584px;
  width: 100%;
  min-height: 94px;
  background: #f5f5f5;
  border-radius: 4px;
  padding: 12px;
}
.player-box-contents {
  display: flex;
  width: 100%;
}
.player-box-left {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  background: #ffffff;
  border-radius: 4px;
}
.solid-item {
  width: 2px;
  height: 18px;
  background: #d9d9d9;
  border-radius: 2px;
}
.solid-1 {
  margin-right: 2px;
  animation: solid-1-move 0.5s linear infinite;
}
.solid-2 {
  margin-right: 2px;
  animation: solid-2-move 0.5s linear infinite;
}
.solid-3 {
  margin-right: 2px;
  animation: solid-3-move 0.5s linear infinite;
}
.solid-4 {
  margin-right: 2px;
  animation: solid-4-move 0.5s linear infinite;
}
.solid-5 {
  margin-right: 2px;
  animation: solid-5-move 0.5s linear infinite;
}
@keyframes solid-1-move {
  from {
    height: 18px;
  }
  to {
    height: 18px;
  }
}
@keyframes solid-2-move {
  from {
    height: 18px;
  }
  to {
    height: 24px;
  }
}
@keyframes solid-3-move {
  from {
    height: 18px;
  }
  to {
    height: 15px;
  }
}
@keyframes solid-4-move {
  from {
    height: 18px;
  }
  to {
    height: 18px;
  }
}
@keyframes solid-5-move {
  from {
    height: 18px;
  }
  to {
    height: 12px;
  }
}
.box {
  animation: scroll 5s linear infinite;
}
.player-box-right {
  display: flex;
  flex-direction: column;
  margin-left: 12px;
  // flex: 1;
  width: calc(100% - 82px);
  padding-right: 4px;
}
.right-top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  .audio-name {
    max-width: 70%;
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.8);
    line-height: 24px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.right-top-menu {
  display: flex;
  align-items: center;
  .doorIcon {
    font-size: 16px;
    line-height: 16px;
  }
  .leftIcon {
    cursor: pointer;
    color: rgba(0, 0, 0, 0.38);
  }
}
.right-top-play {
  display: flex;
  //   margin-left: 16px;
  .doorIcon {
    font-size: 24px;
    line-height: 24px;
    cursor: pointer;
    color: #e62b17;
  }
  .doorIcon:hover {
    color: RGBA(236, 84, 61, 1);
  }
}
.right-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-top: 12px;
}
.right-bottom-left {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.38);
  line-height: 22px;
}
.right-bottom-center {
  margin: 0 12px;
  flex: 1;
  :deep(.t-slider__rail) {
    height: 2px;
  }
  :deep(.t-slider__button) {
    width: 8px;
    height: 8px;
    background: #e62b17;
    border: none;
  }
  :deep(.t-slider__button:hover) {
    width: 10px;
    height: 10px;
    background: #e62b17;
    border: none;
  }
  :deep(.t-slider__track) {
    height: 2px;
    background: #e62b17;
  }
}
.right-bottom-right {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.38);
  line-height: 22px;
}
.right-description {
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.59);
  line-height: 24px;
  overflow: hidden;
  text-overflow: ellipsis;

  display: -webkit-box;

  -webkit-line-clamp: 2;

  -webkit-box-orient: vertical;
}
.edit-box {
  cursor: pointer;
  display: flex;
  align-items: center;
  margin-left: 12px;
  .doorIcon {
    font-size: 16px;

    color: rgba(0, 0, 0, 0.38);
  }
  span {
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.38);
    line-height: 24px;
    margin-left: 4px;
  }
}
.edit-box:hover {
  .doorIcon {
    color: #e62b17;
  }
  span {
    color: #e62b17;
  }
}
</style>
